﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_YQLJ_Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void PopulateYQLJ()
    {
        lvYQLJ.DataSource = YQLJ.GetAllByConditon(txtSearchKey.Text);
        lvYQLJ.DataBind();
    }

    protected void btnSerach_Click(object sender, EventArgs e)
    {
        PopulateYQLJ();
    }
    protected void lvYQLJ_ItemCanceling(object sender, ListViewCancelEventArgs e)
    {
        lvYQLJ.EditIndex = -1;
        PopulateYQLJ();
    }
    protected void lvYQLJ_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "Up")
        {
            int YQLJID = DongDong.Common.Misc.ConvertToInt(e.CommandArgument);
            YQLJ objYQLJ = YQLJ.Get(YQLJID);
            objYQLJ.Rank = System.DateTime.Now;
            objYQLJ.Update();
            PopulateYQLJ();
        }

    }
    protected void lvYQLJ_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        int YQLJID = DongDong.Common.Misc.ConvertToInt(lvYQLJ.DataKeys[e.ItemIndex].Value);
        YQLJ.Delete(YQLJID);
        PopulateYQLJ();
    }
    protected void lvYQLJ_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        lvYQLJ.EditIndex = e.NewEditIndex;
        PopulateYQLJ();
    }

    protected void lvYQLJ_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        int YQLJID = DongDong.Common.Misc.ConvertToInt(lvYQLJ.DataKeys[e.ItemIndex].Value);
        YQLJ objYQLJ = YQLJ.Get(YQLJID);
        var item = lvYQLJ.Items[e.ItemIndex];

        TextBox txtEditTitle = (TextBox)item.FindControl("txtEditTitle");
        FileUpload fuEditImg = (FileUpload)item.FindControl("fuEditImg");
        TextBox txtEdiURL = (TextBox)item.FindControl("txtEdiURL");

        objYQLJ.Title = txtEditTitle.Text;
        if (fuEditImg.HasFile)
            objYQLJ.ImgURL = DongDong.Common.Misc.UploadFile(fuEditImg, "/Resource/YQLJ/", ".jpg.jpeg.png.gif");
        objYQLJ.Link = txtEdiURL.Text;
        objYQLJ.Update();

        lvYQLJ.EditIndex = -1;
        PopulateYQLJ();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        YQLJ objYQLJ = new YQLJ();
        objYQLJ.Title = txtAddName.Text;
        objYQLJ.Link = (txtAddURL.Text ?? "#");
        objYQLJ.Rank = System.DateTime.Now;
        if (fulImg.HasFile)
            objYQLJ.ImgURL = DongDong.Common.Misc.UploadFile(fulImg, "/Resource/YQLJ/", ".jpg.jpeg.png.gif");

        objYQLJ.Add();
        PopulateYQLJ();
    }
    protected void lvYQLJ_PagePropertiesChanged(object sender, EventArgs e)
    {
        PopulateYQLJ();
    }
    protected void Pager_PreRender(object sender, EventArgs e)
    {
        if (lvYQLJ.Items.Count == 0)
            PopulateYQLJ();
    }
}